﻿using BySoft.Server.Business;
using BySoft.Server.Common.Entity;
using BySoft.Web.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BySoft.Web.Site.ComPage
{
    public partial class MaterialListExChange : AuthBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string MaterialID = HttpUtility.UrlDecode(UIHelper.GetQueryStringValue("MaterialID"));
                lblMaterialID.Text = MaterialID;
            }
            Master.ValidationGroup = "gSubmit";
        }

        protected void btnOk_Click(object sender, EventArgs e)
        {
            string newid = this.txtMaterialID.Text.Trim();
            string oldid = this.lblMaterialID.Text.Trim();
            if (!string.IsNullOrEmpty(newid))
            {
                if (!string.IsNullOrEmpty(oldid))
                {
                    MdMaterialEntity en = MdMaterial.GetMdMaterialByID(newid);
                    if (en != null)
                    {
                        if (en.Status.Trim() != "1")
                        {
                            UIHelper.AlertMessage(this.Page, "替换编号已停用,请重新输入！");
                            return;
                        }
                        MdMaterialEntity enold = MdMaterial.GetMdMaterialByID(oldid);
                        if (enold != null)
                        {
                            enold.Status = "0";
                            enold.ModifyDate = DateTime.Now;
                            enold.ModifyUserID = CurrentUser.UserID;
                            enold.ModifyUserName = CurrentUser.UserName;
                            //by zhang 
                            //if (MdMaterial.Update(enold))
                            //{
                            //    if (!string.IsNullOrEmpty(en.MaterialID) && !string.IsNullOrEmpty(en.MaterialName) && !string.IsNullOrEmpty(enold.MaterialID))
                            //    {
                            //        ////更新BS_BomDetail内的MaterialID
                            //        //if (MdMaterial.UpdateBomDetailAndMaintenanceBomDetailMaterialIDByMaterialIDAndName(en.MaterialID, en.MaterialName, enold.MaterialID))
                            //        //{
                            //        //    UIHelper.AlertMessage(this.Page, "替换成功");
                            //        //    ClientScript.RegisterStartupScript(this.GetType(), "", "<script>setCookie(dialogReturn_key,'success',1);CloseTopDialog();</script>");
                            //        //}
                            //        //else
                            //        //{
                            //        //    UIHelper.AlertMessage(this.Page, "替换编号替换BOM表或BOM维护单内数据时出错！");
                            //        //    return;
                            //        //}
                            //    }
                            //    else
                            //    {
                            //        UIHelper.AlertMessage(this.Page, "数据不完整！");
                            //        return;
                            //    }
                            //}
                            //else
                            //{
                            //    UIHelper.AlertMessage(this.Page, "被替换编号状态更改失败！");
                            //    return;
                            //}
                        }
                        else
                        {
                            UIHelper.AlertMessage(this.Page, "被替换编号不存在，请重新选择！");
                            return;
                        }
                    }
                    else
                    {
                        UIHelper.AlertMessage(this.Page, "替换编号不存在，请重新输入！");
                        return;
                    }
                }
                else
                {
                    UIHelper.AlertMessage(this.Page, "请选择被替换编号！");
                    return;
                }
            }
            else
            {
                UIHelper.AlertMessage(this.Page, "请输入替换编号！");
                return;
            }
        }
    }
}